package com.richard.base.util

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.NinePatch
import android.graphics.Rect
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.NinePatchDrawable
import android.view.View
import com.facebook.common.executors.UiThreadImmediateExecutorService
import com.facebook.common.memory.PooledByteBuffer
import com.facebook.common.memory.PooledByteBufferInputStream
import com.facebook.common.references.CloseableReference
import com.facebook.datasource.DataSource
import com.facebook.datasource.DataSubscriber
import com.facebook.drawee.backends.pipeline.Fresco
import com.facebook.drawee.view.SimpleDraweeView
import com.facebook.imagepipeline.datasource.BaseBitmapDataSubscriber
import com.facebook.imagepipeline.datasource.BaseBitmapReferenceDataSubscriber
import com.facebook.imagepipeline.image.CloseableImage
import com.facebook.imagepipeline.request.ImageRequest
import com.richard.base.util.ninepatch.byAddInfo.NinePatchChunk
import com.richard.base.util.ninepatch.byCodePatch.NinePatchBuilder
import java.io.InputStream

/**
 ***************************************
 * 项目名称:Giggle
 * @Author wuzhiguo
 * 邮箱：wuzhiguo@ksjgs.com
 * 创建时间: 2020/8/17     3:08 PM
 * 用途:
 ***************************************
 */
object ImageLoader {
    fun loadImage(view: SimpleDraweeView, url: String) {
        view.setImageURI(url)
    }

    fun loadImage(view: SimpleDraweeView, resourceId: Int) {
        view.setActualImageResource(resourceId)
    }

    fun loadNinePatchBackground(context: Context, view: View, url: String, defaultResId: Int) {
        this.loadNinePatchBackground(context, view, url, defaultResId, Rect())
    }

    fun loadNinePatchBackground(context: Context, view: View, url: String, defaultResId: Int, rect: Rect) {
        val imagePipeline = Fresco.getImagePipeline()
        val imageRequest: ImageRequest? = ImageRequest.fromUri(url)
        val dataSource = imagePipeline.fetchDecodedImage(imageRequest, context)

        dataSource.subscribe(object : BaseBitmapReferenceDataSubscriber(){
            override fun onFailureImpl(dataSource: DataSource<CloseableReference<CloseableImage>>) {
                view.setBackgroundResource(defaultResId)
            }

            override fun onNewResultImpl(bitmapReference: CloseableReference<Bitmap>?) {
                setByCodePatchImage(context, bitmapReference?.get(), view, rect)
            }

            override fun onCancellation(dataSource: DataSource<CloseableReference<CloseableImage>>) {
                super.onCancellation(dataSource)
                view.setBackgroundResource(defaultResId)
            }

        },  UiThreadImmediateExecutorService.getInstance())
    }

    /**
     * 代码设置点9图
     * @param context Context
     * @param bmpTemp Bitmap
     * @param view View
     */
    private fun setByCodePatchImage(context: Context, bmpTemp: Bitmap?, view: View, padding: Rect) {
        if (bmpTemp != null && !bmpTemp.isRecycled) {
            var bmp = Bitmap.createBitmap(bmpTemp)
            val builder = NinePatchBuilder(context.resources, bmp)
            builder.addXCenteredRegion(5)
            builder.addYCenteredRegion(5)
//            val chunk = builder.buildChunk()
//            val ninepatch = builder.buildNinePatch()
//            val drawable = builder.build()
            val drawable = builder.buildWithPadding(padding)
            view.post{
                view.background = drawable
            }
        }
    }

    /**
     * @deprecated use setByCodePatchImage instead
     * 通过已经添加了点9信息的图片设置背景
     * @param context Context
     * @param bmpTemp Bitmap
     * @param view View
     */
    @Deprecated("")
    private fun setByAddInfoImage(context: Context, bmpTemp: Bitmap, view: View) {
        if (bmpTemp != null && !bmpTemp.isRecycled) {
            var bmp = Bitmap.createBitmap(bmpTemp)
            var chunk = bmp?.ninePatchChunk
            if (NinePatch.isNinePatchChunk(chunk)) {
                var ninePatchDrawable =  NinePatchDrawable(context.resources, bmp, chunk,  NinePatchChunk.getPaddingRect(chunk), null)
                view.post {
                    view.background = ninePatchDrawable
                }
            } else {
                var bitmapDrawable = BitmapDrawable(context.resources, bmp)
                view.post {
                    view.background = bitmapDrawable
                }
            }
        }
    }
}